package tianrun.ziguan.api.calculate.config.service.common.edit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.dto.request.common.DeleteBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.SaveBody;
import tianrun.ziguan.api.calculate.config.entity.TUsageAnalysisModule;
import tianrun.ziguan.api.calculate.config.service.ge.TGeTableConfigService;
import tianrun.ziguan.api.calculate.config.service.template.analysis.ITUsageAnalysisModuleService;
import tianrun.ziguan.api.calculate.config.utils.MapUtil;
import tianrun.ziguan.api.calculate.config.utils.StringUtil;

import java.util.*;

@Service
public class UsageAnalysisModuleDealService extends AbstractDealService {

    private final ITUsageAnalysisModuleService usageAnalysisModuleService;

    public UsageAnalysisModuleDealService(TGeTableConfigService tableConfigService, ITUsageAnalysisModuleService usageAnalysisModuleService) {
        super(tableConfigService);

        this.usageAnalysisModuleService = usageAnalysisModuleService;
    }

    @Override
    public void before(SaveBody body) {
        String calLabel = MapUtil.getString(body.getData(), "cal_label");
        if (StringUtil.isEmpty(calLabel))return;
        String templateId = MapUtil.getString(body.getData(), "template_id");
        LambdaQueryWrapper<TUsageAnalysisModule> wrapper = Wrappers.<TUsageAnalysisModule>lambdaQuery().
                eq(TUsageAnalysisModule::getCalLabel, calLabel).
                eq(TUsageAnalysisModule::getTemplateId, templateId);
        if (Objects.equals(body.getOption(), SaveBody.Option.EDIT)) {
            wrapper.ne(TUsageAnalysisModule::getId, body.getId());
        }
        int count = usageAnalysisModuleService.count(wrapper);
        Assert.isTrue(count == 0, calLabel + "已存在！");
    }


    @Override
    public String tableName() {
        return "t_usage_analysis_module";
    }

    @Override
    public void after(SaveBody body) {

    }
    @Override
    public void delBefore(DeleteBody body) {

    }

    @Override
    public void listAfter(List<Map<String, Object>> result) {

    }

    @Override
    public void delAfter(DeleteBody body) {

    }

    @Override
    public void infoAfter(Map<String, Object> bean) {

    }


}
